package com.aptemo.webetek.client.widgets.scoreboard;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.impl.GetPlayersAction;
import com.aptemo.webetek.client.commands.responses.GotPlayersList;
import com.aptemo.webetek.client.enums.EnumUserStatus;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.utils.EarningsComparator;
import com.aptemo.webetek.client.utils.PlacedComparator;
import com.aptemo.webetek.client.utils.WonComparator;
import com.aptemo.webetek.client.widgets.paging.PagingController;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;

public class TeamStandingsPresenter
		extends PagingController<UserBean> implements ClickHandler
{

	public interface Display extends BaseDisplay
	{
		HasClickHandlers getList();

		int getClickedRow(ClickEvent event);

		HasText getInfoField();

		void renderList(List<UserBean> list);

		PagingDisplay getPaging();

		HasClickHandlers getEarningsLink();

		HasClickHandlers getPlacedLink();

		HasClickHandlers getWonLink();

		String translate(String key);
	}

	private HandlerManager					meventBus;
	private WebetekUserServiceAsync			mservice;
	private TeamStandingsPresenter.Display	display;
	private int								pageNo				= 0;
	private String							teamId;

	private List<UserBean>					playersList			= new ArrayList<UserBean>();

	private EarningsComparator				earningsComparator	= new EarningsComparator(false);
	private PlacedComparator				placedComparator	= new PlacedComparator(false);
	private WonComparator					wonComparator		= new WonComparator(false);
	private Comparator<UserBean>			currentComparator	= earningsComparator;

	public TeamStandingsPresenter(Display view, HandlerManager eventbus, WebetekUserServiceAsync rpc, String id)
	{
		this(view, eventbus, rpc, id, 0);
	}

	public TeamStandingsPresenter(Display view, HandlerManager eventBus, WebetekUserServiceAsync rpcService, String teamid, int pageNo)
	{
		super(view.getPaging(), 10);

		this.meventBus = eventBus;
		this.mservice = rpcService;
		this.display = view;
		this.teamId = teamid;
		this.pageNo = pageNo;
	}

	private void bindDisplay()
	{

		display.getEarningsLink().addClickHandler(this);
		display.getPlacedLink().addClickHandler(this);
		display.getWonLink().addClickHandler(this);

		// refresh handler...
		// meventBus.addHandler(BetPlacedEvent.TYPE, new RefreshEventHandler()
		// {
		//
		// @Override
		// public void onRefresh()
		// {
		// retrieveList();
		// }
		// });

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0)
				{
					// get it from paging...
					// UserBean bet = getItem(selectedRow);
					// String id = bet.getKey();
					// meventBus.fireEvent(new SelectedBetEvent(id, teamId));
				}

			}
		});

	}

	private void retrieveList()
	{
		display.getInfoField().setText(display.translate("loading"));

		// retrieve players and statistics...
		GetPlayersAction action = new GetPlayersAction(this.teamId, EnumUserStatus.ACTIVE);
		action.setStats(true);

		mservice.invoke(action, new GotPlayersList()
		{

			@Override
			public void onError(String message)
			{
				display.getInfoField().setText(message);
			}

			@Override
			public void got(ArrayList<UserBean> list)
			{
				// sort it
				Collections.sort(list, currentComparator);

				// save list
				playersList = list;

				renderList(playersList);

				display.getInfoField().setText("");
			}
		});
	}

	private void renderList(List<UserBean> list)
	{

		List<UserBean> arr = new ArrayList<UserBean>();
		for (UserBean score : list)
		{
			arr.add(score);
		}

		setList(arr, pageNo);
	}

	// private boolean statusCheck(EnumBetStatus status, String sel)
	// {
	// return sel.isEmpty() || (status.toString().equals(sel)) ? true : false;
	// }

	@Override
	public void go(HasWidgets container)
	{
		bindDisplay();

		container.clear();
		container.add(display.getDisplay());

		retrieveList();

	}

	@Override
	public void refreshPaging(List<UserBean> data)
	{
		display.renderList(data);
	}

	@Override
	public void onClick(ClickEvent event)
	{
		if (event.getSource() == display.getEarningsLink())
		{
			earningsComparator.setFactor(earningsComparator.getFactor() * -1);
			currentComparator = earningsComparator;
			// retrieveList();
		}
		else if (event.getSource() == display.getPlacedLink())
		{
			placedComparator.setFactor(placedComparator.getFactor() * -1);
			currentComparator = placedComparator;
			// retrieveList();

		}
		else if (event.getSource() == display.getWonLink())
		{
			wonComparator.setFactor(wonComparator.getFactor() * -1);
			currentComparator = wonComparator;
			// retrieveList();
		}
		else
		{
			return;
		}
		// sort it
		Collections.sort(playersList, currentComparator);
		renderList(playersList);
	}

}
